package com.mall507.fivepeaks.activity.comn;

import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.AbsRecyclerViewUI;
import com.hzh.frame.widget.rxbus.MsgEvent;
import com.hzh.frame.widget.rxbus.RxBus;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.mall507.comn.model.Message;
import com.mall507.comn.tools.UserTools;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.activity.main.MainOriginalRFM;
import com.mall507.fivepeaks.widget.Banner.BGABanner;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static android.text.Html.FROM_HTML_MODE_COMPACT;

/**
 * 公共商品详情
 * @date 2019/8/8
 */
@Route(path = "/comn/ComnQuestionItemInfoRUI")
public class ComnQuestionItemInfoRUI extends AbsRecyclerViewUI<Message> {
    @ViewInject(R.id.send) //发送评论
    TextView send;
    @ViewInject(R.id.comment) //评论内容
    EditText comment;
    @ViewInject(R.id.collection)
    LinearLayout collection;

    private int page = 0;

    View headView;
    String views="";
    String comments="";
    String collections="";

    @Override
    protected int setLayoutId() {
        return R.layout.ui_rv_comn_question_item_info;
    }

    @Override
    protected void bindView() {
        getTitleView().setContent("详情");
        showLoding();
        collection.setVisibility(View.GONE);
        headView = getLayoutInflater().inflate(R.layout.head_rv_comn_goods_item_info2, null);
        getAdapter().setHeaderView(headView);
        getHeadData();
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (UserTools.getInstance().getIsLogin()){
                    if (TextUtils.isEmpty(comment.getText())){
                        alert("评论内容不能为空");
                    }else {
                        if (comment.getText().toString().length() >= 6){
                            sendComment();
                        }else {
                            alert("评论内容不足6字");
                        }
                    }
                }else {
                    alert("请登录之后尝试");
                    ARouter.getInstance().build("/login/login").navigation();
                }
            }
        });
    }

    @Override
    protected String setHttpPath() {
        return getIntent().getStringExtra("commentRequestPath");
    }

    @Override
    protected JSONObject setHttpParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }

    @Override
    protected List<Message> handleHttpData(JSONObject response) {
        List<Message> list = new ArrayList<>();
        if (200 == response.optInt("code")) {
            JSONArray jsonArray = response.optJSONArray("data");
            if (jsonArray != null && jsonArray.length() > 0) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject object = jsonArray.optJSONObject(i);
                    JSONObject member = jsonArray.optJSONObject(i).optJSONObject("member");
                    Message model = new Message();
                    model.setNid(object.optString("id"));
                    if (member != null && member.length()>0){
                        model.setHead(member.optString("head_img"));
                        model.setName(member.optString("nickname"));
                    }
                    model.setDesc(object.optString("content"));
                    model.setTime(object.optString("create_at"));
                    list.add(model);
                }
            }
        }
        dismissLoding();
        return list;
    }

    private void getHeadData() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        
        BaseHttp.getInstance().query(getIntent().getStringExtra("headRequestPath"), params, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if(200==response.optInt("code")){
                    final JSONObject data = response.optJSONObject("data");
                        JSONObject params = new JSONObject();
                        try {
                            params.put("type", getIntent().getStringExtra("type"));
                            params.put("id", getIntent().getStringExtra("id"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        BaseHttp.getInstance().query("Action/viewBlog", params, new HttpCallBack() {
                            @Override
                            public void onSuccess(JSONObject response) {
                                if (200 == response.optInt("code")){
                                    JSONObject datas = response.optJSONObject("data");
                                    views = datas.optString("views");
                                }
                                comments = data.optString("comment_count");
                                collections = data.optString("collection_count");
                                ((TextView)headView.findViewById(R.id.head_pageview)).setText(views + "人浏览"); //浏览数
                                super.onSuccess(response);
                            }
                        });
                    if(data!=null){
                        TextView desc = headView.findViewById(R.id.head_content);
                        CharSequence charSequence;
                        String content = data.optString("content");
                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                            charSequence = Html.fromHtml(content, FROM_HTML_MODE_COMPACT);
                        } else {
                            charSequence = Html.fromHtml(content);
                        }
                        desc.setText(charSequence);
                        ((TextView)headView.findViewById(R.id.head_title)).setText(data.optString("title")); //文章标题
//                        ((TextView)headView.findViewById(R.id.head_content)).setText(data.optString("content")); //文章内容
                        ((TextView)headView.findViewById(R.id.head_pageview)).setText(data.optString("views")+"人浏览"); //浏览数
                        ((TextView)headView.findViewById(R.id.head_time)).setText(data.optString("create_at")); //文章发布时间
                    }
                }
            }
        });
    }

    @Override
    protected int setItemLayoutId(int viewType) {
        return R.layout.item_rv_comn_goods_item_info;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, int position, Message model) {
        holder.setText(R.id.name, model.getName());
        holder.setText(R.id.desc, model.getDesc());
        holder.setText(R.id.time, model.getTime());
        ((CircleImageView) holder.getView(R.id.icon)).setImageURI(model.getHead());
    }

    private void sendComment(){
        JSONObject params = new JSONObject();
        try {
            params.put("type", getIntent().getStringExtra("type"));
            params.put("id", getIntent().getStringExtra("id"));
            params.put("content", comment.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        BaseHttp.getInstance().query("comment/subProductComment", params, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (200 == response.optInt("code")){
                    comment.getText().clear();
                    onRefresh();
                    alert(response.optString("msg"));
                    JSONObject data = response.optJSONObject("data");
                    comments = data.optString("comment_count");
                    setEvent(collections,comments,views);
                }else {
                    alert(response.optString("msg"));
                }
                super.onSuccess(response);
            }
        });
    }

    private void setEvent(String collection,String comment,String views){
        HashMap<String,Object> map = new HashMap<>();
        map.put("position",getIntent().getStringExtra("position"));
        map.put("collection",collection);
        map.put("views",views);
        map.put("comment",comment);
        MsgEvent msgEvent=new MsgEvent(MainOriginalRFM.TAG,new Gson().toJson(map));
        RxBus.getInstance().post(msgEvent);
    }

}
